package com.qen.http

import cn.hutool.core.net.multipart.UploadFile
import cn.hutool.http.ContentType
import cn.hutool.http.HttpUtil
import cn.hutool.http.server.HttpServerRequest
import cn.hutool.http.server.HttpServerResponse
import cn.hutool.http.server.action.Action
import cn.hutool.json.JSONUtil

fun main() {
    HttpUtil.createServer(8888)
        .addAction("/", Action { req: HttpServerRequest, res: HttpServerResponse ->
            res.write("Hello  World")
        })
        .addAction("/restTest", Action { req, res ->
            val json = JSONUtil.createObj().set("id", 1).set("msg", "OK")
            res.write(json.toString(), ContentType.JSON.toString())
        })
        .addAction("/formTest", Action { req, res ->
            res.write(req.getParams().toString(), ContentType.TEXT_PLAIN.toString())
        })
        .addAction("/file", Action { req, res ->
            val file = req.multipart.getFile("file")
            file.write("/Users/a1/Documents/Git/Kotlin/Eleven/file/")
            res.write("OK!", ContentType.TEXT_PLAIN.toString())
        })
        .start()
}

